package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;

import antlr.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Module;
import com.sl.au.service.ModuleService;

@RestController
@RequestMapping(value = "/ui/module")
public class ModuleController extends BaseController {

	@Autowired
	ModuleService moduleService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = createLayoutView("module/moduleindex");

		return modelAndView;
	}

	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map getPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
//		String moduleName='%'+key+'%';
		List<Module> modules=null;
		Long count=null;
		if(org.springframework.util.StringUtils.hasText(key)){
			modules = moduleService.getModules(start, length, key, null, null);
			count=moduleService.getRepository().getModuleCount(key);
		}
		else{
			modules = moduleService.getPage(start, length, null, null, null);
			String j="2";
			count=moduleService.getRepository().getCount(j);
		}
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	///获得一个菜单的信息byID
	@RequestMapping(value="getNode",method = RequestMethod.GET)
	public Map<String,Object> getNode(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		String moduleId=request.getParameter("moduleId");
		Module node=moduleService.getRepository().findOne(moduleId);
		map.put("node", node);
		return map;
	}
	//显示模型的所有节点和模型图片ID
	@RequestMapping(value="getAllNodes",method = RequestMethod.GET)
	public Map<String,Object> getAllNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		String menuId=request.getParameter("menuId");
		List<Module> node=null;
		node=moduleService.getAllNodes(menuId);
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
		nodeMap=moduleService.getNodeMap(node);
		map.put("code", 1);
		map.put("nodes", nodeMap);

		return map;
	}
	//显示节点下一级的子节点
	@RequestMapping(value="/getNextNodes")
	public Map getFirstNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		String pId=request.getParameter("parentId");
		List<Module> nodes=moduleService.getNextNodes(pId);
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();

		for(Module p:nodes){
			if(null!=p){
				Module module=new Module();
				String menuId=p.getMenuId();
				if(org.springframework.util.StringUtils.hasText(menuId)){

				}
				Map<String,Object> map1=new HashMap<String,Object>();
				map1.put("id", p.getId());
				map1.put("name", module.getModuleName());
				nodeMap.add(map1);
			}
		}
		map.put("code", 1);
		map.put("nodes", nodeMap);
		return map;
	}
	//保存节点
	@RequestMapping(value="/saveNode")
	public Map saveNode(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		String menuId=request.getParameter("menuId");
		String parentId=request.getParameter("parentId");
		String id=request.getParameter("id");
		Module node=new Module();
		node.setParentId(parentId);
		node.setMenuId(menuId);
		String nodeId=id;
		moduleService.saveNode(menuId,parentId,id,node);
		//将增加的节点的父节点设置isParent和open
		moduleService.setParentNode(parentId,"true");
		map.put("code", 1);
		map.put("nodeId",nodeId);
		return map;
	}
	//删除模型的节点
	@RequestMapping(value="/deleteNodes")
	public Map deleteNode(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		String str=request.getParameter("str");
		String selectPId=request.getParameter("selectPId");
		String ids[]=str.split(",");
		moduleService.deleteNodes(ids);
		int isParent=moduleService.sureParent(selectPId);
		if(isParent==1){
			moduleService.setParentNode(selectPId, "true");
		}
		else if(isParent==0){
			moduleService.setParentNode(selectPId, "false");
		}
		map.put("code", 1);
		return map;
	}
}
